package com.end;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Exam5 {
    Socket socket;
    public Scanner sc = new Scanner(System.in);

    Exam5() throws Exception{
        socket = new Socket(InetAddress.getLocalHost(), 6666);
    }
    public static void main(String[] args) throws Exception {
        Exam5 exam5 = new Exam5();

        while(true){
            System.out.println("欢迎进入系统，请选择你需要的功能序号");
            System.out.println("1.登录");
            System.out.println("2.注册");
            int order = exam5.sc.nextInt();
            exam5.sc.nextLine();
            if(order == 1){
                exam5.login();
            }else if(order == 2){
                exam5.zhuce();
            }
        }

    }
    public void zhuce() throws Exception{
        System.out.println("注册界面：");
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
        while(true){
            System.out.println("请输入用户名");
            String id = sc.nextLine();
            System.out.println("请输入密码");
            String passwd = sc.nextLine();
            outputStream.writeUTF("注册|" + id + "|" + passwd);
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            String result = inputStream.readUTF();
            if(result.equals("true")){
                System.out.println("注册成功");
                return;
            }else System.out.println("用户名已被注册");
        }
    }
    public void login() throws Exception{
        System.out.println("登录界面：");
        DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
        while(true){
            System.out.println("请输入用户名");
            String id = sc.nextLine();
            System.out.println("请输入密码");
            String passwd = sc.nextLine();
            outputStream.writeUTF("登录|" + id + "|" + passwd);
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            String result = inputStream.readUTF();
            if(result.equals("true")){
                System.out.println("登陆成功");
                return;
            }else System.out.println("输入错误");
        }

    }
}
